package fourfaith.com.ff_sl21_app;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import fourfaith.com.ff_sl21_app.utils.Constants;
import fourfaith.com.ff_sl21_app.utils.JsonRequest;
import fourfaith.com.ff_sl21_app.utils.RequestQueueSingleton;
import fourfaith.com.ff_sl21_app.utils.ResponseResult;

public class VideoDetailActivity extends AppCompatActivity {


    private Context context;

    private String siteCode,photoUrl,stnm;
    private Integer dsfl;

    private ImageView siteState;
    private TextView siteName;
    private ImageView back;
    private DecimalFormat df = new DecimalFormat("#.###");

    private LinearLayout mapParam,rootLinear,emptyValue,loadValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_detail);

        stnm = getIntent().getStringExtra("siteName");
        siteCode = getIntent().getStringExtra("siteCode");
        photoUrl = getIntent().getStringExtra("photoUrl");
        dsfl = getIntent().getIntExtra("siteState",0);

        initViews();

        siteName.setText(stnm);
        siteState.setImageResource((Integer)dsfl);
        getDataRequest();
    }

    private void initViews() {
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        siteState = (ImageView) findViewById(R.id.siteState);
        siteName = (TextView) findViewById(R.id.siteName);

        rootLinear = (LinearLayout) findViewById(R.id.rootLinear);
        emptyValue = (LinearLayout) findViewById(R.id.emptyValue);
        loadValue = (LinearLayout) findViewById(R.id.loadValue);

        loadValue.setVisibility(View.VISIBLE);
        emptyValue.setVisibility(View.GONE);
        rootLinear.setVisibility(View.GONE);
    }

    public void checkedChangeListener(View v) {
        String param = v.getTag().toString();

        Toast.makeText(this, param, Toast.LENGTH_SHORT).show();

    }

    /**
     * 接口获取数据
     * @return
     */
    private void getDataRequest() {

        String userId = Constants.getUserId();

        String url = Constants.readtimeDataByStcd +"?userId=" + userId + "&stcd="+siteCode;

        RequestQueue mQueue = RequestQueueSingleton.getInstance(context).getRequestQueue();
        JsonRequest<ResponseResult> jsonRequest = new JsonRequest<>(Request.Method.GET, url, ResponseResult.class,
                new Response.Listener<ResponseResult>() {
                    @Override
                    public void onResponse(ResponseResult responseResult) {
                        if (responseResult.getCode().equals("200")) {

                            rootLinear.removeAllViews();

                            JSONObject dataObj = (JSONObject) responseResult.getData();

                            JSONArray viewHead = dataObj.getJSONArray("viewHead");
                            JSONArray sysUserFactorList = dataObj.getJSONArray("sysUserFactorList");

                            LinearLayout playout = new LinearLayout(context);
                            if(!dataObj.get("model").equals("")){
                                JSONObject model = dataObj.getJSONObject("model");
                                loadValue.setVisibility(View.GONE);
                                emptyValue.setVisibility(View.GONE);
                                rootLinear.setVisibility(View.VISIBLE);
                                for (int i=0; i<viewHead.size();i++){

                                    JSONObject jObj = sysUserFactorList.getJSONObject(i);
                                    String factor = jObj.getString("factor");
                                    BigDecimal v = model.getBigDecimal(factor);

                                    LinearLayout layout = new LinearLayout(context);
                                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                                    layoutParams.height=Constants.dip2px(context,30);
                                    layout.setLayoutParams(layoutParams);
                                    TextView fname = new TextView(context);
                                    TextView fvalue = new TextView(context);

                                    fname.setLayoutParams(layoutParams);
                                    fname.setTextColor(ContextCompat.getColorStateList(context, R.color.factor_name_color));
                                    fname.setTextSize(14);
                                    fname.setText(viewHead.get(i)+"：");
                                    fname.setGravity(Gravity.CENTER|Gravity.END);

                                    layout.addView(fname);

                                    fvalue.setLayoutParams(layoutParams);
                                    fvalue.setTextColor(ContextCompat.getColorStateList(context, R.color.factor_value_color));
                                    fvalue.setTextSize(14);
                                    fvalue.setText(v==null?"":df.format(v.doubleValue()));
                                    fvalue.setGravity(Gravity.CENTER|Gravity.START);
                                    layout.addView(fvalue);

                                    playout.addView(layout);
                                    if(i%2==1 || i==viewHead.size()-1){

                                        if(i%2==0 && i==viewHead.size()-1){
                                            //插入空布局
                                            LinearLayout layout2 = new LinearLayout(context);
                                            layout2.setLayoutParams(layoutParams);
                                            playout.addView(layout2);
                                        }

                                        rootLinear.addView(playout);

                                        playout = new LinearLayout(context);

                                        LinearLayout.LayoutParams playoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                                        playoutParams.height=Constants.dip2px(context,30);
                                        playout.setLayoutParams(playoutParams);
                                        playout.setOrientation(LinearLayout.HORIZONTAL);

                                    }


                                }

                                //添加采集时间
                                LinearLayout layout = new LinearLayout(context);
                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 3);
                                layoutParams.height=Constants.dip2px(context,30);
                                layout.setLayoutParams(layoutParams);
                                TextView fname = new TextView(context);
                                TextView fvalue = new TextView(context);

                                fname.setLayoutParams(layoutParams);
                                fname.setTextColor(ContextCompat.getColorStateList(context, R.color.factor_name_color));
                                fname.setTextSize(14);
                                fname.setText("采集时间：");
                                fname.setGravity(Gravity.CENTER|Gravity.END);

                                layout.addView(fname);

                                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                                layoutParams2.height=Constants.dip2px(context,30);
                                fvalue.setLayoutParams(layoutParams2);
                                fvalue.setTextColor(ContextCompat.getColorStateList(context, R.color.factor_value_color));
                                fvalue.setTextSize(14);
                                fvalue.setText(model.get("tm").toString());
                                fvalue.setGravity(Gravity.CENTER|Gravity.START);
                                layout.addView(fvalue);

                                playout.addView(layout);
                                rootLinear.addView(playout);
                            }else{
                                emptyValue.setVisibility(View.VISIBLE);
                                rootLinear.setVisibility(View.GONE);
                                loadValue.setVisibility(View.GONE);
                            }

                        } else {
                            Toast.makeText(context, responseResult.getMsg(), Toast.LENGTH_SHORT).show();
                        }


                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                emptyValue.setVisibility(View.VISIBLE);
                rootLinear.setVisibility(View.GONE);
                loadValue.setVisibility(View.GONE);
                if (error.networkResponse == null) {
                    Toast.makeText(context, "连接超时，请求不到网络数据", Toast.LENGTH_SHORT).show();
                } else {
                    if (error.getMessage() == null) {
                        Toast.makeText(context, "接口服务器异常", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }

            }

        });
        mQueue.add(jsonRequest);

    }
}
